package com.dhecp.project.storage.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.project.storage.domain.TsCargomaintenance;
import com.dhecp.project.storage.mapper.TsCargomaintenanceMapper;
import com.dhecp.project.storage.service.TsCargomaintenanceService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 货物报关信息维护(TsCargomaintenance表)服务实现类
 *
 * @author fxz
 * @since 2024-01-11 15:35:19
 */
@Service
public class TsCargomaintenanceServiceImpl implements TsCargomaintenanceService {
    @Autowired
    private TsCargomaintenanceMapper tsCargomaintenanceMapper;
    
    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    /**
     * 通过guid查询单条数据
     *
     * @param params 对象
     * @return 实例对象
     */
    @Override
    public TsCargomaintenance queryFirstRecord(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return this.tsCargomaintenanceMapper.selectFirstRecordByNid(params);
    }

    /**
    * 查询列表数据
    */
    @Override
    public List<TsCargomaintenance> queryList(Map<String, Object> params) throws Exception{
        params.put(DataFiltering.TABLE_ALIAS, "t1");	//查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));	//数据权限查询条件SQL语句
        return this.tsCargomaintenanceMapper.select(params);
    }


    /**
     * 编辑货物报关信息
     * @author fxz
     * @date: 2023-09-22
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult saveData(Map<String, Object> params) {
        if (StringUtils.isEmpty(params)) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        TsCargomaintenance entity = JSON.parseObject(params.get("infoMap").toString(), TsCargomaintenance.class);

        if (entity.getCargocode() == null || entity.getCargocode().length() == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("数据提交失败，必填参数有误！");
        }

        int count = tsCargomaintenanceMapper.countByCode(entity);
        if (count != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("单据编码重复,请核实再操作！");
        }
        if (entity.getBdeleted() == null){
            entity.setBdeleted(String.valueOf(1));
        }
        if (entity.getGuid() != null && !entity.getGuid().equals("")) {
            entity.setUpdatetime(new Date());
            tsCargomaintenanceMapper.update(entity);
        }else {
            entity.setGuid(RandomValueUtils.getGUID());
            entity.setCreatetime(new Date());
            tsCargomaintenanceMapper.insert(entity);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"),entity.getGuid());
    }


    /**
     * 删除
     * @author fxz
     */
    @Override
    @Transactional(timeout=120,propagation= Propagation.REQUIRES_NEW)
    public AjaxResult delete(Map<String, Object> params) {
        List<TsCargomaintenance> tsCargomaintenanceList = JSON.parseArray(params.get("infoItems").toString(), TsCargomaintenance.class);
        if (tsCargomaintenanceList == null || tsCargomaintenanceList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (TsCargomaintenance tsCargomaintenance : tsCargomaintenanceList) {
            tsCargomaintenanceMapper.delete(tsCargomaintenance);
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}

